#include "AppIniFile.h"

CAppIniFile::CAppIniFile()
    :m_bIsPrintToConsole(true)
    , m_bIsWriteLog(false)
    , m_iLogFileMaxSize(5)
    , m_iLogFileNameType(0)
    , m_usPort(502)
    , m_iIdleTime(10)
    , m_uaModbusAddrOffset(0)
    , m_iProjectMode(0)
{

}

CAppIniFile::~CAppIniFile()
{
    Clear();
}

CAppIniFile::CAppIniFile(const CAppIniFile& that)
{
    if (this == &that)
    {
        return;
    }

    CopyFrom(&that);
}

CAppIniFile& CAppIniFile::operator=(const CAppIniFile& that)
{
    if (this != &that)
    {
        CopyFrom(&that);
    }

    return *this;
}

void CAppIniFile::CopyFrom(const CAppIniFile* pthat)
{
    if (0 == pthat)
    {
        return;
    }

    this->m_bIsPrintToConsole = pthat->m_bIsPrintToConsole;
    this->m_bIsWriteLog = pthat->m_bIsWriteLog;
    this->m_iLogFileMaxSize = pthat->m_iLogFileMaxSize;
    this->m_iLogFileNameType = pthat->m_iLogFileNameType;

    this->m_usPort = pthat->m_usPort;
    this->m_iIdleTime = pthat->m_iIdleTime;
    this->m_uaModbusAddrOffset = pthat->m_uaModbusAddrOffset;
    this->m_iProjectMode = pthat->m_iProjectMode;
}

void CAppIniFile::Clear()
{
    this->m_bIsPrintToConsole = true;
    this->m_bIsWriteLog = false;
    this->m_iLogFileMaxSize = 0;
    this->m_iLogFileNameType = 0;

    this->m_usPort = 0;
    this->m_iIdleTime = 0;
    this->m_uaModbusAddrOffset = 0;
    this->m_iProjectMode = 0;
}

bool CAppIniFile::operator==(const CAppIniFile& that) {
    // 防止自身比较
    if (this == &that) return true;

    //任何一个变量值不等,都返回false
    if (this->m_bIsPrintToConsole != that.m_bIsPrintToConsole) return false;
    if (this->m_bIsWriteLog != that.m_bIsWriteLog) return false;
    if (this->m_iLogFileMaxSize != that.m_iLogFileMaxSize) return false;
    if (this->m_iLogFileNameType != that.m_iLogFileNameType) return false;

    if (this->m_usPort != that.m_usPort) return false;
    if (this->m_iIdleTime != that.m_iIdleTime) return false;
    if (this->m_uaModbusAddrOffset != that.m_uaModbusAddrOffset) return false;
    if (this->m_iProjectMode != that.m_iProjectMode) return false;

    // 以上变量值都相等,返回true
    return true;
}

bool CAppIniFile::operator!=(const CAppIniFile& that) {
    return (!(*this == that));
}

void CAppIniFile::ReadFromIniFile()
{
    // read xntransrv.ini
    QSettings setFile("../ini/xntransrv.ini", QSettings::IniFormat);

    m_bIsPrintToConsole = (1 == setFile.value("LOG/IsPrintToConsole", 1).toInt())?true:false;
    m_bIsWriteLog = (1 == setFile.value("LOG/IsWriteLog", 0).toInt())?true:false;
    m_iLogFileMaxSize = setFile.value("LOG/LogFileMaxSize", 0).toInt();
    CheckIntRange(m_iLogFileMaxSize, 1, 30); //最小1兆,最大30兆
    //文件名称类型,0为abc.log,1为abc(yyyy).log,2为abc(yyyyMM).log,3为abc(yyyyMMdd).log,默认为0
    m_iLogFileNameType = setFile.value("LOG/LogFileNameType", 1).toInt();
    CheckIntRange(m_iLogFileNameType, 0, 5); //最小0,最大5

    m_usPort = (ushort)setFile.value("TCP/Port", 502).toInt();
    m_iIdleTime = setFile.value("TCP/ClientIdleTime", 10).toInt();
    CheckIntRange(m_iIdleTime, 5, 1000); //最小5毫秒,最大1秒
    m_uaModbusAddrOffset = (uchar)setFile.value("TCP/ModbusAddrOffset", 0).toUInt();
    m_iProjectMode = setFile.value("TCP/ProjectMode", 0).toInt();
    CheckIntRange(m_iProjectMode, 0, 2); //最小0,最大2
}

//获取描述信息
QString CAppIniFile::GetRemark()
{
    QString str = "\n";

    str += QString("从(../ini/xntransrv.ini)中读取参数 LOG/IsPrintToConsole=%1\n").arg(m_bIsPrintToConsole?1:0);
    str += QString("从(../ini/xntransrv.ini)中读取参数 LOG/IsWriteLog=%1\n").arg(m_bIsWriteLog?1:0);
    str += QString("从(../ini/xntransrv.ini)中读取参数 LOG/LogFileMaxSize=%1\n").arg(m_iLogFileMaxSize);
    str += QString("从(../ini/xntransrv.ini)中读取参数 LOG/LogFileNameType=%1\n").arg(m_iLogFileNameType);

    str += QString("从(../ini/xntransrv.ini)中读取参数 TCP/Port=%1\n").arg(m_usPort);
    str += QString("从(../ini/xntransrv.ini)中读取参数 TCP/ClientIdleTime=%1\n").arg(m_iIdleTime);
    str += QString("从(../ini/xntransrv.ini)中读取参数 TCP/ModbusAddrOffset=%1\n").arg(m_uaModbusAddrOffset);
    str += QString("从(../ini/xntransrv.ini)中读取参数 TCP/ProjectMode=%1\n").arg(m_iProjectMode);

    return str;
}

//验证变量的值范围(小于最小值则取最小值,大于最大值则取最大值)
void CAppIniFile::CheckUlonglongRange(qulonglong& ulValue, qulonglong ulMinValue, qulonglong ulMaxValue)
{
    if (ulValue < ulMinValue) {
        //最小值
        ulValue = ulMinValue;
    } else if (ulValue > ulMaxValue) {
        //最大值
        ulValue = ulMaxValue;
    }
}

void CAppIniFile::CheckIntRange(int& iValue, int iMinValue, int iMaxValue)
{
    if (iValue < iMinValue) {
        //最小值
        iValue = iMinValue;
    } else if (iValue > iMaxValue) {
        //最大值
        iValue = iMaxValue;
    }
}

bool CAppIniFile::GetIsPrintToConsole()
{
    return m_bIsPrintToConsole;
}

void CAppIniFile::SetIsPrintToConsole(bool bIsPrintToConsole)
{
    m_bIsPrintToConsole = bIsPrintToConsole;
}

bool CAppIniFile::GetIsWriteLog()
{
    return m_bIsWriteLog;
}

void CAppIniFile::SetIsWrieLog(bool bIsWriteLog)
{
    m_bIsWriteLog = bIsWriteLog;
}

int CAppIniFile::GetLogFileMaxSize()
{
    return m_iLogFileMaxSize;
}

void CAppIniFile::SetLogFileMaxSize(int iLogFileMaxSize)
{
    m_iLogFileMaxSize = iLogFileMaxSize;
}

int CAppIniFile::GetLogFileNameType()
{
    return m_iLogFileNameType;
}

void CAppIniFile::SetLogFileNameType(int iLogFileNameType)
{
    m_iLogFileNameType = iLogFileNameType;
}

ushort CAppIniFile::GetPort()
{
    return m_usPort;
}

int CAppIniFile::GetIdleTime()
{
    return m_iIdleTime;
}

uchar CAppIniFile::GetModbusAddOffset()
{
    return m_uaModbusAddrOffset;
}

int CAppIniFile::GetProjectMode()
{
    return m_iProjectMode;
}
